package ttc.sftp.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class UploadService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Toast.makeText(getApplicationContext(), "create service", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Toast.makeText(getApplicationContext(), "service destroy", Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		
		String username = intent.getStringExtra("usernameKey");
		String password = intent.getStringExtra("passwordKey");
		String host = intent.getStringExtra("hostKey");
		String portSent = intent.getStringExtra("portKey");
		int port =Integer.parseInt(portSent);		
		String filePath = intent.getStringExtra("fullfileparth");
		String filename = intent.getStringExtra("fileName");
		
		
		
		
		
		try {
			upload(username, password, host, port, filePath, filename);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "io excption", Toast.LENGTH_SHORT).show();
		}
		
		Toast.makeText(getApplicationContext(), "update complete", Toast.LENGTH_SHORT).show();
		
	}
	
	
	//ham upload
	public void upload(String username,String password,String host,int port,String filePath,String fileName) throws IOException{
		JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect(); 

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            try {
            	
            	

          //  	File sdCard = Environment.getExternalStorageDirectory();
        //    	File dir = new File(sdCard.getAbsolutePath());
       //     	File file = new File(dir,"mysdfile.txt");
            	File file = new File(filePath);
            	
            	FileInputStream fi = new FileInputStream(file);
            	
            	sftpChannel.put(fi, fileName);
            	
            	
            }catch (Exception e) {
				// TODO: handle exception
			}
            sftpChannel.exit();
            session.disconnect(); 
        } catch (JSchException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            Toast.makeText(getApplicationContext(), "ham ket noi co van de", Toast.LENGTH_SHORT).show();
            
        }
	}
	
	
	

}
